package com.ecarx.vhaltests.hy11.testcases.ota;

import static android.car.VehiclePropertyIds.SEAT_TILT_POS;

import static com.ecarx.xui.adaptapi.car.property.IPropertyValue.EPadlInhibitStatus.UNINHIBIT;

import android.car.VehicleAreaType;
import android.car.hardware.CarPropertyValue;
import android.car.hardware.property.CarPropertyManager;
import android.util.Log;

import com.ecarx.vhaltests.VehicleConstants;
import com.ecarx.vhaltests.VhalUnitTestBase;
import com.ecarx.vhaltests.VhalUnitTestBase2;
import com.ecarx.vhaltests.hy11.testcases.Utils.EcuUtil;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.concurrent.atomic.AtomicInteger;

import vendor.ecarx.xma.automotive.vehicle.V2_0.DirectTransmitProperty;
import vendor.ecarx.xma.automotive.vehicle.V2_0.YesNo2;

/**
 * 外部诊断仪状态
 */
public class DiagcExtComTest extends VhalUnitTestBase2 {
    private static final String TAG = "DiagcExtComs";
    private final AtomicInteger mCallbackStatus = new AtomicInteger(Integer.MIN_VALUE);

    @Override
    public void init() {
        super.init();
        getCarPropertyMgr().registerCallback(mCallback, DirectTransmitProperty.DiagcExtCom, CarPropertyManager.SENSOR_RATE_ONCHANGE);
    }
    @BeforeClass
    public static void testInit() {
        EcuUtil.registerConfigCDM(getIpcpClient());
    }

    @Test
    public void testDiagcExtComNo() {
        setCDM(YesNo2.No);
        lockWait();
        Assert.assertEquals(mCallbackStatus.get(), YesNo2.No);
        int value = getCarPropertyMgr().getIntProperty(getPropertyId(), VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL);
        Assert.assertEquals(YesNo2.No, value);

    }

    @Test
    public void testDiagcExtComYes() {
        setCDM(YesNo2.Yes);
        lockWait();
        Assert.assertEquals(mCallbackStatus.get(), YesNo2.Yes);
        int value = getCarPropertyMgr().getIntProperty(getPropertyId(), VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL);
        Assert.assertEquals(YesNo2.Yes, value);
    }


    public void setCDM(int value) {
        String[] requests = {
                "DiagcExtCom"
        };
        int[] values = {
                value
        };
        EcuUtil.setCDM(getIpcpClient(), requests, values);
    }

    private CarPropertyManager.CarPropertyEventCallback mCallback = new CarPropertyManager.CarPropertyEventCallback() {
        @Override
        public void onChangeEvent(CarPropertyValue carPropertyValue) {
            try {
                Log.e(TAG, carPropertyValue.toString());
                if (carPropertyValue.getPropertyId() != DirectTransmitProperty.DiagcExtCom) {
                    return;
                }

                Object obj = carPropertyValue.getValue();
                if (obj == null) {
                    Log.e(TAG, "DirectTransmitProperty.DiagcExtCom Value is NULL");
                    return;
                }

                if (!(obj instanceof Integer)) {
                    Log.e(TAG, "DirectTransmitProperty.DiagcExtCom Value is Not Integer");
                    return;
                }
                mCallbackStatus.set((Integer) obj);
                lockNotify();

            } catch (Exception e){
                e.printStackTrace();
            }
        }

        @Override
        public void onErrorEvent(int i, int i1) {

        }
    };

    @Override
    protected int getPropertyId() {
        return DirectTransmitProperty.DiagcExtCom;
    }

    @Override
    protected int getArea() {
        return VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL;
    }

    @Override
    protected String getValueType() {
        return TYPE_VALUE_INT;
    }
}
